package com.cdtu.controller;

import com.cdtu.anno.Log;
import com.cdtu.mapper.DeptMapper;
import com.cdtu.pojo.Dept;
import com.cdtu.pojo.Emp;
import com.cdtu.pojo.Result;
import com.cdtu.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.List;
//@Lazy
//@Scope("prototype")
@Slf4j
@RestController  //@ResponseBody将返回值转换为JSON格式数据
@RequestMapping("/depts")
public class DeptController {

    public DeptController() {
        System.out.println("DeptController Constructor...");
    }

    @Autowired
    private DeptService deptService;


//    @RequestMapping(value = "/depts", method = RequestMethod.GET)
    @GetMapping
    public Result list(){
        log.info("查询全部部门数据");
        List<Dept> deptList = deptService.list();
//        System.out.println(deptService);
        return Result.success(deptList);
    }
    @Log
    @DeleteMapping("/{id}")
    public Result delete( @PathVariable Integer id) throws Exception {
        log.info("根据id删除{}",id);
        deptService.delete(id);
        return Result.success();
    }
    @Log
    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("新增部门：{}",dept);
        deptService.add(dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result selectById( @PathVariable Integer id){
        log.info("根据ID查询：{}",id);
        Dept dept = deptService.selectById(id);
        return Result.success(dept);
    }

    @Log
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("修改部门信息{}",dept);
        deptService.update(dept);
        return Result.success();
    }



}
